﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Metadata.Edm;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data;

namespace Anata.Client
{
    public static class Common
    {
        public static EntitySetBase GetEntitySet(this ObjectContext context, Type entityType)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (entityType == null)
            {
                throw new ArgumentNullException("entityType");
            }

            EntityContainer container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);

            if (container == null)
            {
                return null;
            }

            EntitySetBase entitySet = container.BaseEntitySets.Where(item => item.ElementType.Name.Equals(entityType.Name))
                                                              .FirstOrDefault();

            return entitySet;
        }

        public static void ApplyCurrentValues(this ObjectContext context, Type entityType)
        {
        }

        //public static void AttachUpdated(this ObjectContext obj, EntityObject objectDetached)
        //{
        //    if (objectDetached.EntityState == EntityState.Detached)
        //    {
        //        object original = null;
        //        if (obj.TryGetObjectByKey(objectDetached.EntityKey, out original))
        //            obj.ApplyPropertyChanges(objectDetached.EntityKey.EntitySetName, objectDetached);
        //        else
        //            throw new ObjectNotFoundException();
        //    }
        //}        
    }
}
